﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionFactory.Services;
using SolutionFactory.UI;
using SolutionFactory.VSTemplateSchema;

namespace SolutionFactory.Exporter
{
    public class InstallerCreationService
    {
        private readonly IProcessExecutor _processExecutor;
        private readonly IFileTokenReplacer _fileTokenReplacer;
        private readonly FileCopyAndTokenReplacer _fileCopyAndTokenReplacer;
        private readonly IFileSystem _fileSystem;
        private readonly ILogger _logger;

        public InstallerCreationService(IProcessExecutor processExecutor,IFileTokenReplacer fileTokenReplacer,IFileSystem fileSystem,ILogger logger)
        {
            _processExecutor = processExecutor;
            _fileTokenReplacer = fileTokenReplacer;            
            _fileSystem = fileSystem;
            _logger = logger;
        }

        public void CreateInstaller(TemplateInput model, string templateZipFilename)
        {
            CreateContentPackage(model, templateZipFilename);
        }

        private void CreateContentPackage(TemplateInput model, string templateZipFilename)
        {
            string addinDir = _fileSystem.GetDirectoryName(GetType().Assembly.Location);
            
            string toolSourceDir = addinDir.Combine("tools");
            string tempDirectory = _fileSystem.CreateTempDirectory();            
            _fileSystem.Copy(templateZipFilename, tempDirectory.Combine("template.zip"));

            //copy bins and wix template to the temp dir
            _fileSystem.Copy(addinDir.Combine("SolutionFactory.Wizard.dll"),tempDirectory.Combine("SolutionFactory.Wizard.dll"));
            _fileSystem.Copy(addinDir.Combine("template.wxs"), tempDirectory.Combine("template.wxs"));
            _fileSystem.Copy(addinDir.Combine("solutionfactory.ico"), tempDirectory.Combine("solutionfactory.ico"));
            _fileSystem.CopyFilesAndDirectories(toolSourceDir, tempDirectory.Combine("Tools"));
            

            //replace the tokens in the wix template.
            _fileTokenReplacer.SetTokens(CreateTokens(model));
            _fileTokenReplacer.Replace(tempDirectory.Combine("template.wxs"));
                        
            
            //Execute the wix commands.
            string directoryName = _fileSystem.GetDirectoryName(templateZipFilename);
            string fileName = directoryName.Combine(_fileSystem.GetFileName(templateZipFilename) + ".msi" );

            _logger.Log("Creating Template Installer" + fileName);
            _processExecutor.Execute(tempDirectory.Combine(@"tools\wix\candle.exe"), tempDirectory, "-out Template.wixobj template.wxs", false);
            _processExecutor.Execute(tempDirectory.Combine(@"tools\wix\light.exe"), tempDirectory, "-out \""+ fileName+"\" Template.wixobj", false);
            _logger.Log("Template Installer " + fileName + " created.");
        }

        private Dictionary<string, string> CreateTokens(TemplateInput model)
        {
            var dictionary = new Dictionary<string, string>();
            dictionary.Add("$PRODUCT-GUID$",Guid.NewGuid().ToString());
            dictionary.Add("$UPGRADE-GUID$", Guid.NewGuid().ToString());
            dictionary.Add("$TEMPLATE-NAME$", model.Name);
            dictionary.Add("$CATEGORY$", model.Category);
            dictionary.Add("$SUB-CATEGORY$", model.SubCategory);
            dictionary.Add("$COMPONENT-GUID$", Guid.NewGuid().ToString());
            return dictionary;
        }

    }
}
